<?php
class StoreManageAction extends CommonAction{
	public function _filter(&$map){
		$map['removed']=1;
		$_REQUEST ['_order']='storename';
	}

    public function _before_index() {
		$model = M ( "StoreManage" );
		$list = $model->getField ( 'id,whp,stock,lpm,we,tw,rw,product');
		$this->assign ( 'groupList', $list );
	}
	public function read(){
		$id=$_REQUEST['id'];
		$model = M ( "StoreManage" );
		$vo=$model->where('id='.$id)->find();
		$this->assign ( 'vo', $vo );
		$this->display();
		
	}
	public function _before_add() {
		$model = M ( "StoreManage" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
		$Goods=M('Goods');
		$goodslist=$Goods->select();
		$this->assign('goods',$goodslist);
	}
	public function _before_edit() {
		$Goods=M('Goods');
		$goodslist=$Goods->select();
		$this->assign('goods',$goodslist);
	}
	public function _before_insert() {
		$model = M ( "StoreManage" );
		$storename=$_REQUEST['storename'];
	    $StoreManage = $model->where('storename=\''.$storename.'\' and product=\''.$_POST ['product'].'\'')->select();	   
		if($StoreManage){
		   if($StoreManage[0]['removed']==2){
			  $model->where('id='.$StoreManage[0]['id'])->delete();
		   }else{
			  $this->ajaxReturn ( 0, '该 商品名称 已经存在!', 300 );
		}
		}
	}
	public function _before_update(){
		$model = M ( "StoreManage" );
		$StoreManage = $model->find ( $_POST ['id'] );
		if ($StoreManage ['product'] != $_POST ['product']) {
			$map['product'] = $_POST ['product'];
			//$map['removed'] = 1;
			$map['storename'] = $StoreManage['storename'];
			$result = $model->where ($map)->select ();
			if ($result) {
				if($result[0]['removed']==2){
					$model->where('id='.$result[0]['id'])->delete();
				}else{
					$this->ajaxReturn ( 0, '该 商品名称 已经存在!', 300 );
				}
			}
		}
	}
	public function storeA(){
		$model=M('StoreManage');
		import ( '@.Org.Page' );
		$count = $model->where("storename='storeA' and removed=1")->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where("storename='storeA'  and removed=1")->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	public function storeC(){
		$model=M('StoreManage');
		import ( '@.Org.Page' );
		$count = $model->where("storename='storeC' and removed=1")->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where("storename='storeC'  and removed=1")->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	public function storeB(){
		$model=M('StoreManage');
		import ( '@.Org.Page' );
		$count = $model->where("storename='storeB'  and removed=1")->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where("storename='storeB' and removed=1")->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	//显示东明库回收站中的记录
	public function storeAtrash(){
		$model = D ('StoreManage');
		import ( '@.Org.Page' );
		$count = $model->where('storename=\'storeA\' and removed=2')->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where('storename=\'storeA\' and removed=2')->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	//显示南曹库回收站中的记录
	public function storeBtrash(){
		$model = D ('StoreManage');
		import ( '@.Org.Page' );
		$count = $model->where('storename=\'storeB\' and removed=2')->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where('storename=\'storeB\' and removed=2')->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	//显示金海库回收站中的记录
	public function storeCtrash(){
		$model = D ('StoreManage');
		import ( '@.Org.Page' );
		$count = $model->where('storename=\'storeC\' and removed=2')->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model->where('storename=\'storeC\' and removed=2')->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	
	//合成新产品
	public function addp()
	{
		if($_REQUEST['parameter']=='insertp'){
			$this->insertp();
		}else{
			if($_REQUEST['parameter']=='getgoods'){
				$this->getgoods();
			}else{
				$id=$_REQUEST['id'];
				$storename=$_REQUEST['storename'];
				$stm=M('StoreManage');
				$list=$stm->where('id='.$id)->find();
				$this->assign("sm",$list);
				$this->assign("id",$id);
				$this->assign("storename",$storename);
				$this->display();
			}
		}
	}
	public function getgoods()
	{
		$storename=$_REQUEST['storename'];
		$accessory=M('Accessory');
		import ( '@.Org.Page' );
		$count =$accessory->where('storeplace=\''.$storename.'\'')->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 24 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list=$accessory->where('storeplace=\''.$storename.'\'')->page ( $pageNum . "," . $page->listRows )->order('id desc')->select();
		$this->assign('list',$list);
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->assign ( 'storename', $storename );
		$this->display('getgoods');
	}
	
	public function insertp()
	{
		
		$pid=$_REQUEST['pid'];
		$storename=$_REQUEST['storename'];
		$whp=$_REQUEST['whp'];
		$product=$_REQUEST['product'];
		$capacity=$_REQUEST['capacity'];
		$note=$_REQUEST['note'];//备注
		$sm=M('StoreManage');
		$stm=$sm->where('id='.$pid)->find();
		if(!empty($capacity)&&!empty($stm['stock']))
		{
			if($capacity>$stm['stock'])
			{
				$this->ajaxReturn(0,'录入吨数大于仓库中的库存量',300);
			}
		}
		else
		{
			$this->ajaxReturn(0,'录入吨数或者仓库中的库存量为0',300);
		}
		
		$accessQuality=$this->AddMerchandise($storename,$product);
		
		$stock=$accessQuality+$capacity;
		$data['whp']=$whp;
		$data['product']=$product;
		$data['storename']=$storename;
		$data['stock']=$stock;
		$data['note']=$note;
		$good=M('Goods');
		
		$list=$sm->where('product=\''.$product.'\' and storename=\''.$storename.'\'')->select();
		if(!empty($list))
		{
			$liststock=$list[0]['stock'];
			$listid=$list[0]['id'];
			$listdata['stock']=$liststock+$stock;
			$result=$sm->where('id='.$listid)->save($listdata);	
		}
		else
		{
		  $result=$sm->add($data); 
		}
		$mapdata['stock']=$stm['stock']-$capacity;
		$result2=$sm->where('id='.$pid)->save($mapdata);
		if($result&&$result2)
		{
			$glist=$good->where('goodsname=\''.$product.'\'')->select();
			if(empty($glist))
			{
				$gdata['goodsname']=$product;
				$good->add($gdata);
			}
			$this->ajaxReturn(0,'保存成功',200);
		}
		else 
		{
			$this->ajaxReturn(0,'保存失败',300);
		}
		
		
	}
	
	
	/**
	 * 获得辅料信息包括种类，吨数，单价，总额
	 */
	public function AddMerchandise($storename,$product){
		$model = M('Accessory');
		$goodname = $_POST['items_org_goodname'];
		$quality = $_POST['items_itemQuality'];
		//$price = $_POST['items_itemPrice'];
		//$totalprice = $_POST['items_itemTotalprice'];
		$totalquality=0;
		if(count($goodname)>=0){
			for($i=0 ; $i < count($goodname);$i++){
				//$data['bussinessid']=$list;
				$data['name']=$goodname[$i];
				$data['storeplace']=$storename;
				$dataquality=$quality[$i];
				//$data['quality']=$quality[$i];
				//$data['price']=$price[$i];
				//$data['totalprice']=$totalprice[$i];
				//$model->add($data);
				$list=$model->where($data)->select();
				$weight=$list[0]['weight'];
				$rest=$weight-$dataquality;
				if(empty($weight))
				{
					$this->assign(0,'辅料库存为0',300);
				}
				else if($rest<0)
				{
					$this->assign(0,'输入的吨数大于辅料的余量',300);
				}
				$map['weight']=$rest;
				$model->where($data)->save($map);
				$aslog=M('AccessoryStock');
				$alog['accid']=$list[0]['id'];
				$alog['accname']=$list[0]['name'];
				$alog['stock']=$dataquality;
				$alog['time']=date('YmdHis');
				$alog['type']=2;
				$alog['note']='用于合成'.$product;
				$result=$aslog->add($alog);
				if($result){
				  $totalquality+=$dataquality;
				}
				 
				
			}
		}
		return $totalquality;
	}
}

?>
